package com.shunbo.convenient.controller;


import com.shunbo.convenient.service.StoreService;
import com.shunbo.convenient.service.UserInfoService;
import com.shunbo.convenient.utils.ConvenientResult;
import com.shunbo.convenient.vo.UserInfo;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * @author weizhuang
 * B端用户
 */
@RestController
@RequestMapping("/userInfo")
public class UserInfoController {


    @Resource
    private UserInfoService userInfoService;
    @Resource
    private StoreService storeService;


    /**
     * 用户信息列表
     * @param userInfo 用户信息列表
     * @return Result结果集
     */
    @RequestMapping("/list")
    public ConvenientResult findUserInfoList(@RequestBody UserInfo userInfo){
        try {
            List<UserInfo> all = userInfoService.findAll(userInfo);
            return ConvenientResult.build(200,"成功",all);
        }catch (Exception e){
            e.printStackTrace();
            return ConvenientResult.build(500,"系统错误",e.getStackTrace());
        }
    }

    /**
     * 添加用户信息列表
     * @param userInfo 用户信息
     * @return Result结果集
     */
    @RequestMapping("/save")
    public ConvenientResult saveUserInfo(@RequestBody UserInfo userInfo){
        try {
            String userId = UUID.randomUUID().toString().replaceAll("-","");
            userInfo.setUserId(userId);
            Integer save = userInfoService.save(userInfo);
            userInfo.getStore().setUser_id(userId);
            storeService.save(userInfo.getStore());
            return ConvenientResult.build(200,"成功",save);
        }catch (Exception e){
            e.printStackTrace();
            return ConvenientResult.build(500,"系统错误",e.getStackTrace());
        }
    }

    /**
     * 更新用户信息
     * @param userInfo 用户信息
     * @return Result结果集
     */
    @RequestMapping("/update")
    public ConvenientResult updateUserInfo(@RequestBody UserInfo userInfo){
        try {
            Integer save = userInfoService.update(userInfo);
            return ConvenientResult.build(200,"成功",save);
        }catch (Exception e){
            e.printStackTrace();
            return ConvenientResult.build(500,"系统错误",e.getStackTrace());
        }
    }

    /**
     * 删除用户信息
     * @param userInfo 用户信息
     * @return Result结果集
     */
    @RequestMapping("/delete")
    public ConvenientResult deleteUserInfo(@RequestBody UserInfo userInfo){
        try {
            Integer save = userInfoService.delete(userInfo.getId()+"");
            return ConvenientResult.build(200,"成功",save);
        }catch (Exception e){
            e.printStackTrace();
            return ConvenientResult.build(500,"系统错误",e.getStackTrace());
        }
    }

    /**
     * 通过id查找用户信息
     * @param userInfo 用户信息
     * @return Result结果集
     */
    @RequestMapping("/findOne")
    public ConvenientResult findUserInfoById(@RequestBody UserInfo userInfo){
        try {
            UserInfo byId = userInfoService.findById(userInfo.getId() + "");
            return ConvenientResult.build(200,"成功",byId);
        }catch (Exception e){
            e.printStackTrace();
            return ConvenientResult.build(500,"系统错误",e.getStackTrace());
        }
    }


}
